package com.yoomiito.app.gift;

import androidx.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.gift.GiftGoods;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.UserInfoConfig;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

public class YouPinAdapter extends BaseQuickAdapter<GiftGoods, BaseViewHolder> {
    //用于区分不同的页面  目前在优品 和 礼包页面使用了GiftAdapter
    //不同的页面 佣金 奖 券 等的显示规则不同 用mPageType来进行区分
    private int mGoodsType;

    public YouPinAdapter(@Nullable List<GiftGoods> data, int PageType) {
        super(R.layout.item_gife_goods, data);
        this.mGoodsType = PageType;
    }

    @Override
    protected void convert(BaseViewHolder helper, GiftGoods item) {

        //product_type 0-表示正常VIP产品，1-表示3650Vip产品,2-爆品',
        ImageView goodsIv = helper.getView(R.id.item_gift_pic);
        ImageView iv = helper.getView(R.id.goodsFlag);
        TextView titleTv = helper.getView(R.id.goodsTitle);
        View awardView = helper.getView(R.id.item_gift);
        TextView couponTv = helper.getView(R.id.tv_bonus);
        TagFlowLayout tagFlowLayout = helper.getView(R.id.item_gift_flowLayout);
        List<String> s = new ArrayList<>();
        if (!TextUtils.isEmpty(item.getProduct_point())) {
            s.add(item.getProduct_point());
        }
        tagFlowLayout.setAdapter(new TagAdapter<String>(s) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView tv = (TextView) LayoutInflater.from(tagFlowLayout.getContext()).inflate(R.layout.item_goods_flag, parent, false);
                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) tv.getLayoutParams();
                lp.leftMargin = 0;
//                tv.setLayoutParams(lp);
                tv.setText(o.length()>8?(o.substring(0,8)+"..."):o);
                return tv;
            }
        });
        TextView priceTv = helper.getView(R.id.item_gift_goods_price);
        TextView oldPriceTv = helper.getView(R.id.item_gift_goods_old_price);
        TextView saleSizeTv = helper.getView(R.id.item_gift_goods_sale_size);

        ImageLoader.getInstance().into(goodsIv.getContext(), item.getProduct_img(), goodsIv);


        //大礼包标记
        iv.setVisibility(View.GONE);

        titleTv.setText(item.getProduct_name());
        helper.setGone(R.id.item_coupon_3,false);

        if (AllCode.YouPin == mGoodsType||AllCode.AgentMall==mGoodsType) {//优品 爆品
            String price;
            if (item.getYo_money_not() == 0) {//优米为0不显示
                price = SpannableUtils.handlePrice(item.getSale_price());
                priceTv.setText(price);
            } else {
                price = SpannableUtils.handlePrice(item.getCash()) + " + " + item.getYo_money_not() + "优米";
                int addFlag = price.indexOf("+");
                SpannableString spanText = new SpannableString(price);
                spanText.setSpan(new RelativeSizeSpan(0.75f), addFlag, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                priceTv.setText(spanText);
            }
            if (App.mAppConfig.getIsLogin()){
                //奖标记
                if (SpannableUtils.isEmptyOr0(item.getCommission())) {
                    awardView.setVisibility(View.GONE);
                } else {//粉丝以上显示奖
//                    awardView.setVisibility(UserInfoConfig.getRoleId() > 0 ? View.VISIBLE : View.GONE);
                    couponTv.setText("¥" + SpannableUtils.handlePrice(item.getCommission()));
                }
            }else {
                awardView.setVisibility(View.GONE);
            }
        }else {
            LinearLayout songLl = helper.getView(R.id.song_layout);
            if (item.getYo_money()>0){
                songLl.setVisibility(View.VISIBLE);
                helper.setText(R.id.song_value,item.getYo_money()+"优米");
            }else {
                songLl.setVisibility(View.GONE);
            }
            priceTv.setText(SpannableUtils.handlePrice(item.getSale_price()));
            //奖金
            if (App.mAppConfig.getIsLogin()){
                //奖标记
                if (SpannableUtils.isEmptyOr0(item.getCommission())) {
                    awardView.setVisibility(View.GONE);
                } else {//粉丝以上显示奖
                    awardView.setVisibility(UserInfoConfig.getRoleId() > 0 ? View.VISIBLE : View.GONE);
                    couponTv.setText("¥" + SpannableUtils.handlePrice(item.getCommission()));
                }
            }else {
                awardView.setVisibility(View.GONE);
            }
        }
//        oldPriceTv.setText(SpannableUtils.isEmptyOr0(item.getMarket_price()) ? "专享价" : (SpannableUtils.getSpannable("¥" + SpannableUtils.handlePrice(item.getMarket_price()), 0)));

        saleSizeTv.setText("已售" + SpannableUtils.saleNum(item.getVolume()));
    }
}
